/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


/*************************************************************************
 * File: iocp.h
 *************************************************************************
 *\author: starlove
 *
 *************************************************************************/

#ifdef __MLSE_PLATFORM_WIN__

#ifndef __MLSE_NETWORK_IOCP_H__
#define __MLSE_NETWORK_IOCP_H__

#include <network/network.h>
#include <network/event_iocp.h>
#include <network/ovmgr.h>
#include <tools/fixed_allocator.h>

#define X_SOCK_MAINVERSION 			2
#define X_SOCK_SUBVERSION 			2
#define X_SOCK_ACCEPT_QUEUE_LIMIT 	100
#define X_EVENT_LIMIT 				64

enum XOperationType
{
    OP_READ = 0,
    OP_WRITE,
    OP_ACCEPT
};

struct XACCEPTOVERLAPPED : public XOVERLAPPED
{
    SOCKET	listen_socket;
    char	szAddrBuf[2 * (sizeof(sockaddr_in) + 16)];
};

typedef XACCEPTOVERLAPPED *LPXACCEPTOVERLAPPED;

struct XSOCKETOVERLAPPED : public XOVERLAPPED
{
    WSABUF				wbuf;
    DWORD				dwBytes, dwFlags;
};

typedef XSOCKETOVERLAPPED *LPXSOCKETOVERLAPPED;

class CMSNetwork : public INetwork
{
public:
    bool Init();
    bool Destory();
    static LPFN_ACCEPTEX GetAcceptExPtr();
    static unsigned int GetSocketCount();
    static bool EnableIOCP(SOCKET s);
    static SOCKET CreateIOCPTCPSocket();
    static SOCKET CreateTCPSocket();
    static void CloseSocket(SOCKET s);
    static void SetSocketOpt( SOCKET s, int linger_flag = 0 );
    static sockaddr *BuildSockAddr( const char *szAddr, unsigned int uPort );
    static void GetAcceptExSockaddr(void *buf, sockaddr *pSelf, sockaddr *pRemote);
};
extern volatile long g_nSocketCount;
extern LPFN_ACCEPTEX g_lpfnAcceptEx;

class CMSSocket : public ISocket
{
public:
    CMSSocket();
    ~CMSSocket();
    bool Init(CEventDispatcher *ep);
    void BindProcessor(IEventProcessor *pp);
    void BindIO(SOCKET s);
    bool Destory();
    void Accept(SOCKET s_listen, IEventProcessor *pp);
    SOCKET Connect(const char *szAddr, unsigned int uPort);
    SOCKET Listen(unsigned int uPort, const char *szAddr = NULL);
    void GetSockAddr(LPEVENT ev, sockaddr *pSelfAddr, sockaddr *pRemoteAddr);
    void Close(SOCKET s);
    void Close(LPEVENT ev);
    int Send(SOCKET &s, void *pData, unsigned int nSize);
    int Recv(SOCKET &s, void *pBuffer, unsigned int nBufferSize);
    int IOSend(SOCKET s, IPeer *pPeer, IEventProcessor *pp = NULL);
    int IORecv(SOCKET s, IPeer *pPeer, IEventProcessor *pp = NULL);
    IEventIO *GetEventIO(){ return m_ep->GetIO(); };
protected:
private:
    IEventProcessor *m_pp;
    CEventDispatcher *m_ep;
    CFixedAllocator<XACCEPTOVERLAPPED> m_Accept_Overlappeds;
    CFixedAllocator<XSOCKETOVERLAPPED> m_Socket_Overlappeds;
};

#endif
#endif
